package com.pactera.miyuangroup.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.pactera.miyuangroup.db.entity.GoodsClassification;
import com.pactera.miyuangroup.db.entity.LoginUser;
import com.pactera.miyuangroup.db.mapper.GoodsClassificationMapper;
import com.pactera.miyuangroup.service.GoodsClassificationService;
import com.pactera.miyuangroup.service.MiyuanDataService;
import com.pactera.miyuangroup.vo.response.miyuan.Classification;
import com.pactera.miyuangroup.vo.response.miyuan.MiYuanModel;
import com.pactera.miyuangroup.vo.response.miyuan.MiYuanPage;
import com.pactera.miyuangroup.vo.response.miyuan.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 商品分类 服务实现类
 * </p>
 *
 * @author WangWu
 * @since 2018-04-13
 */
@Service
public class GoodsClassificationServiceImp extends ServiceImpl<GoodsClassificationMapper, GoodsClassification> implements GoodsClassificationService {
    @Autowired
    private MiyuanDataService miyuanDataService;

    @Override
    public boolean syncMiyuanGoodsClassificationInfo() {

        MiYuanModel<MiYuanPage<Classification>> infos = miyuanDataService.getGoodsClassification(1, 1000);
        //是否成功
        //失败，跳出，继续下一页
        if (!infos.isSuccess()) {
            return false;
        }

        //验证是否有数据，没有数据 跳出 继续下一页
        if (infos.getData() == null || infos.getData().getRecords() == null && infos.getData().getRecords().size() <= 0) {
            return false;
        }
        //更新集合
        List<GoodsClassification> upInfos = new ArrayList<>();
        //新增集合
        List<GoodsClassification> instInfos = new ArrayList<>();
        //循环数据入库
        for (Classification cInfo : infos.getData().getRecords()) {
            GoodsClassification goodsClassification = selectById(cInfo.getId());
            if (null == goodsClassification) {
                goodsClassification = new GoodsClassification();
                goodsClassification.setId(cInfo.getId());
                goodsClassification.setName(cInfo.getName());
                goodsClassification.setPid(cInfo.getPid());
                goodsClassification.setOriginalId(cInfo.getId());
                instInfos.add(goodsClassification);
            } else {
                goodsClassification.setName(cInfo.getName());
                goodsClassification.setPid(cInfo.getPid());
                goodsClassification.setOriginalId(cInfo.getId());
                upInfos.add(goodsClassification);
            }

        }
        //更新
        if (upInfos.size() > 0) {
            updateBatchById(upInfos, upInfos.size());
        }
        //新增
        if (instInfos.size() > 0) {
            insertBatch(instInfos, instInfos.size());
        }
        return true;
    }
}
